package per.chenxin.tools.foundation;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

public class ClasspathWindow extends Dialog {

	protected Object result;
	protected Shell shell;
	protected List listClasspath;

	/**
	 * Create the dialog
	 * 
	 * @param parent
	 * @param style
	 */
	public ClasspathWindow(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * 
	 * @param parent
	 */
	public ClasspathWindow(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {

		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(500, 402);
		shell.setText("Classpath");

		listClasspath = new List(shell, SWT.V_SCROLL | SWT.MULTI | SWT.H_SCROLL
				| SWT.BORDER);
		listClasspath.setBounds(10, 10, 474, 323);

		final Button buttonAddFile = new Button(shell, SWT.NONE);
		buttonAddFile.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
				dialog.open();
				String[] files = dialog.getFileNames();

				if (files != null)
					for (int i = 0; i < files.length; i++)
						Util.getClasspathSet(false).add(
								dialog.getFilterPath() + File.separator
										+ files[i]);
				loadClasspath(false);
			}
		});
		buttonAddFile.setText("Add&File");
		buttonAddFile.setBounds(60, 339, 51, 22);

		final Button buttonDelete = new Button(shell, SWT.NONE);
		buttonDelete.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				String[] strs = listClasspath.getSelection();
				for (int i = 0; i < strs.length; i++)
					Util.getClasspathSet(false).remove(strs[i]);

				listClasspath.remove(listClasspath.getSelectionIndices());
			}
		});
		buttonDelete.setText("&Delete");
		buttonDelete.setBounds(282, 339, 44, 22);

		final Button buttonSave = new Button(shell, SWT.NONE);
		buttonSave.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				try {
					Util.saveClasspathSet();
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
					Util.showErrorMessage(shell, "FileNotFoundException+"
							+ e1.getMessage());
				}
				loadClasspath(true);
			}
		});
		buttonSave.setText("&Save");
		buttonSave.setBounds(386, 339, 44, 22);

		final Button buttonAddPath = new Button(shell, SWT.NONE);
		buttonAddPath.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(shell);
				String path = dialog.open();
				if (path != null) {
					Util.getClasspathSet(false).add(path);
					loadClasspath(false);
				}
			}
		});
		buttonAddPath.setBounds(171, 339, 51, 22);
		buttonAddPath.setText("Add&Path");
		//
		loadClasspath(true);
	}

	private void loadClasspath(boolean isReload) {
		listClasspath.removeAll();
		Set set = Util.getClasspathSet(isReload);
		Object[] objs = set.toArray();
		for (int i = 0; i < objs.length; i++)
			listClasspath.add((String) objs[i]);
	}

}
